package jp.bgbt.fles.app.wifi;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import com.common.utils.LogUtil;
import com.common.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import jp.bgbt.fles.app.bean.EventMsg;
import jp.bgbt.fles.app.bluetooth.BleDeviceInfo;

public class WifiStateReceiver extends BroadcastReceiver {

    private String TAG = getClass().getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
                BleDeviceInfo bleDeviceInfo = BleDeviceInfo.getInstance();
                LogUtil.e("wifi断开，从设备wifi断开？ ---- " + bleDeviceInfo.isDeviceWifiConnected());
                if (/*!StringUtils.isEmpty(bleDeviceInfo.getWifiAddr()) && */bleDeviceInfo.isDeviceWifiConnected()) {
//                    bleDeviceInfo.setWifiAddr("");
                    bleDeviceInfo.setDeviceWifiConnected(false);
                    EventBus.getDefault().post(new EventMsg(EventMsg.MSG_WIFI_DISCONNECTED));
                }
            } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                BleDeviceInfo bleDeviceInfo = BleDeviceInfo.getInstance();
                try {
                    LogUtil.e("连接到网络 " + wifiInfo.getSSID() + " / 设备wifi？ " +
                            wifiInfo.getSSID().contains(bleDeviceInfo.getRealWifiSSID()) + " / " +
                            "设备WIFI已连接？ " + BleDeviceInfo.getInstance().isDeviceWifiConnected());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String mac = bleDeviceInfo.getWifiAddr();
                if (wifiInfo != null && !StringUtils.isEmpty(mac) &&
                        wifiInfo.getSSID().contains(bleDeviceInfo.getRealWifiSSID())) {
                    if (!BleDeviceInfo.getInstance().isDeviceWifiConnected()) {
                        // 连接到了设备的wifi
                        BleDeviceInfo.getInstance().setDeviceWifiConnected(true);
                        EventBus.getDefault().post(new EventMsg(EventMsg.MSG_WIFI_CONNECTED));
                    }
                }
            }

            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
                int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);
                if (wifistate == WifiManager.WIFI_STATE_DISABLED) {
                    LogUtil.e("系统关闭wifi");
                } else if (wifistate == WifiManager.WIFI_STATE_ENABLED) {
                    LogUtil.e("系统开启wifi");
                }
            }
        }
    }
}